package com.epalmpay.service.thread;

import com.epalmpay.entity.ActivityUnroutineRule;
import com.epalmpay.enumdef.ActivityEnum;
import com.epalmpay.mapper.ActivityBaseMapper;
import com.epalmpay.mapper.ActivityUnroutineRuleMapper;

import java.util.List;

public class CheckRedpackageActivityRuleThread implements Runnable {
    private Long redpackageId;
    private ActivityUnroutineRuleMapper activityUnroutineRuleMapper;
    private ActivityBaseMapper activityBaseMapper;

    public CheckRedpackageActivityRuleThread(Long redpackageId, ActivityUnroutineRuleMapper activityUnroutineRuleMapper,
                                         ActivityBaseMapper activityBaseMapper) {
        this.redpackageId = redpackageId;
        this.activityUnroutineRuleMapper = activityUnroutineRuleMapper;
        this.activityBaseMapper = activityBaseMapper;
    }

    @Override
    public void run() {
        List<ActivityUnroutineRule> activityUnroutineRules = activityUnroutineRuleMapper.listByRuleContainRedpackageId(redpackageId);
        if(activityUnroutineRules.size() > 0) {

            activityUnroutineRuleMapper.deleteByRedpackageId(redpackageId);

            for(int i = 0; i < activityUnroutineRules.size(); i++) {
                ActivityUnroutineRule activityUnroutineRule = activityUnroutineRules.get(i);
                int count = activityUnroutineRuleMapper.countRuleByActivityId(activityUnroutineRule.getActivityId());
                if(count == 0) {
                    activityBaseMapper.updateStatusByPrimaryKey(activityUnroutineRule.getActivityId(), ActivityEnum.ActivityStatus.Over.getType());
                }
            }
        }
    }
}
